package zpx.util;
import org.dom4j.*;
import org.dom4j.io.*;

import zpx.model.DataDictionaryItems;

import java.io.File;
import java.util.*;
 

public class XMLDDItemParser {
 

  //数据字典项结构
  public static DataDictionaryItems dataItems ;
  private static String GROUP_NAME = "name";
  private static String GROUP_CODE = "value";
  private static String ITEM_NAME = "name";
  private static String ITEM_CODE = "value";
 

  public XMLDDItemParser() {
  }
 
    /**
   * 获得分组数据字典项集
   * @param groupName String
   * @return DataDictionaryItems
   */
  public static DataDictionaryItems getDataDictionaryItems(String groupName)
  {
    if(dataItems == null)
      dataItems = parseXML(groupName);
    return dataItems;
  }
 
  /**
   * 根据分组名称解析xml文件，获得该分组下数据字典项集
   * @param gName String
   * @return DataDictionaryItems 数据字典项分组对象
   */
  @SuppressWarnings("rawtypes")
public static DataDictionaryItems parseXML(String gName)
  {
    try
    {
      org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();
      Document document = saxReader.read(new File("D:/Git/zpxmaven/zpxmaven/src/main/resources/DataDictionaryConfig.xml"));
 

      dataItems = new DataDictionaryItems();
 
      List list =  document.selectNodes("//group");
      Iterator iter = list.iterator();
      while (iter.hasNext())
      {
        Node node = (Node) iter.next();
        if (node instanceof Element)
        {
          //document
          Element element = (Element) node;
          String GroupName = element.attributeValue(GROUP_NAME);
          String GroupValue = element.attributeValue(GROUP_CODE);
          //设置分组名称编码
          dataItems.setGroupName(GroupName);
          dataItems.setGroupCode(GroupValue);
          //取组内数据字典项
          if (gName.equals(GroupName))
          {
            //取数据字典项名称编码
            Iterator elemIter = element.elementIterator();
            while (elemIter.hasNext())
            {
              Element elem = (Element) elemIter.next();
              dataItems.setDataDictionaryItem(elem.attributeValue(ITEM_NAME), elem.attributeValue(ITEM_CODE));
            }
          }
        }
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return dataItems;
  }
  
  
}